package org.apache.harmony.javax.security.auth.login;

import java.io.IOException;
import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.security.Security;
import java.util.HashMap;
import java.util.Map;
import org.apache.harmony.javax.security.auth.AuthPermission;
import org.apache.harmony.javax.security.auth.Subject;
import org.apache.harmony.javax.security.auth.callback.Callback;
import org.apache.harmony.javax.security.auth.callback.CallbackHandler;
import org.apache.harmony.javax.security.auth.callback.UnsupportedCallbackException;
import org.apache.harmony.javax.security.auth.spi.LoginModule;

public class LoginContext
{
  private static final String DEFAULT_CALLBACK_HANDLER_PROPERTY = "auth.login.defaultCallbackHandler";
  private static final int OPTIONAL = 0;
  private static final int REQUIRED = 1;
  private static final int REQUISITE = 2;
  private static final int SUFFICIENT = 3;
  private Subject subject;
  private boolean userProvidedSubject;
  private boolean userProvidedConfig;
  private AccessControlContext userContext;
  private CallbackHandler callbackHandler;
  private Module[] modules;
  private Map<String, ?> sharedState;
  private ClassLoader contextClassLoader;
  private boolean loggedIn;

  public LoginContext(String paramString)
    throws LoginException
  {
    init(paramString, null, null, null);
  }

  public LoginContext(String paramString, CallbackHandler paramCallbackHandler)
    throws LoginException
  {
    if (paramCallbackHandler == null)
      throw new LoginException("auth.34");
    init(paramString, null, paramCallbackHandler, null);
  }

  public LoginContext(String paramString, Subject paramSubject)
    throws LoginException
  {
    if (paramSubject == null)
      throw new LoginException("auth.03");
    init(paramString, paramSubject, null, null);
  }

  public LoginContext(String paramString, Subject paramSubject, CallbackHandler paramCallbackHandler)
    throws LoginException
  {
    if (paramSubject == null)
      throw new LoginException("auth.03");
    if (paramCallbackHandler == null)
      throw new LoginException("auth.34");
    init(paramString, paramSubject, paramCallbackHandler, null);
  }

  public LoginContext(String paramString, Subject paramSubject, CallbackHandler paramCallbackHandler, Configuration paramConfiguration)
    throws LoginException
  {
    init(paramString, paramSubject, paramCallbackHandler, paramConfiguration);
  }

  private void init(String paramString, Subject paramSubject, final CallbackHandler paramCallbackHandler, Configuration paramConfiguration)
    throws LoginException
  {
    this.userProvidedSubject = ((this.subject = paramSubject) != null);
    if (paramString == null)
      throw new LoginException("auth.00");
    if (paramConfiguration == null)
      paramConfiguration = Configuration.getAccessibleConfiguration();
    else
      this.userProvidedConfig = true;
    SecurityManager localSecurityManager = System.getSecurityManager();
    if ((localSecurityManager != null) && (!this.userProvidedConfig))
      localSecurityManager.checkPermission(new AuthPermission("createLoginContext." + paramString));
    AppConfigurationEntry[] arrayOfAppConfigurationEntry = paramConfiguration.getAppConfigurationEntry(paramString);
    if (arrayOfAppConfigurationEntry == null)
    {
      if ((localSecurityManager != null) && (!this.userProvidedConfig))
        localSecurityManager.checkPermission(new AuthPermission("createLoginContext.other"));
      arrayOfAppConfigurationEntry = paramConfiguration.getAppConfigurationEntry("other");
      if (arrayOfAppConfigurationEntry == null)
        throw new LoginException("auth.35 " + paramString);
    }
    this.modules = new Module[arrayOfAppConfigurationEntry.length];
    for (int i = 0; i < this.modules.length; i++)
      this.modules[i] = new Module(arrayOfAppConfigurationEntry[i]);
    try
    {
      AccessController.doPrivileged(new PrivilegedExceptionAction()
      {
        public Void run()
          throws Exception
        {
          LoginContext.this.contextClassLoader = Thread.currentThread().getContextClassLoader();
          if (LoginContext.this.contextClassLoader == null)
            LoginContext.this.contextClassLoader = ClassLoader.getSystemClassLoader();
          if (paramCallbackHandler == null)
          {
            String str = Security.getProperty("auth.login.defaultCallbackHandler");
            if ((str == null) || (str.length() == 0))
              return null;
            Class localClass = Class.forName(str, true, LoginContext.this.contextClassLoader);
            LoginContext.this.callbackHandler = ((CallbackHandler)localClass.newInstance());
          }
          else
          {
            LoginContext.this.callbackHandler = paramCallbackHandler;
          }
          return null;
        }
      });
    }
    catch (PrivilegedActionException localPrivilegedActionException)
    {
      Throwable localThrowable = localPrivilegedActionException.getCause();
      throw ((LoginException)new LoginException("auth.36").initCause(localThrowable));
    }
    if (this.userProvidedConfig)
    {
      this.userContext = AccessController.getContext();
    }
    else if (this.callbackHandler != null)
    {
      this.userContext = AccessController.getContext();
      this.callbackHandler = new ContextedCallbackHandler(this.callbackHandler);
    }
  }

  public Subject getSubject()
  {
    if ((this.userProvidedSubject) || (this.loggedIn))
      return this.subject;
    return null;
  }

  public void login()
    throws LoginException
  {
    PrivilegedExceptionAction local2 = new PrivilegedExceptionAction()
    {
      public Void run()
        throws LoginException
      {
        LoginContext.this.loginImpl();
        return null;
      }
    };
    try
    {
      if (this.userProvidedConfig)
        AccessController.doPrivileged(local2, this.userContext);
      else
        AccessController.doPrivileged(local2);
    }
    catch (PrivilegedActionException localPrivilegedActionException)
    {
      throw ((LoginException)localPrivilegedActionException.getException());
    }
  }

  private void loginImpl()
    throws LoginException
  {
    if (this.subject == null)
      this.subject = new Subject();
    if (this.sharedState == null)
      this.sharedState = new HashMap();
    Object localObject1 = null;
    int[] arrayOfInt1 = new int[4];
    int[] arrayOfInt2 = new int[4];
    for (Module localModule : this.modules)
    {
      try
      {
        localModule.create(this.subject, this.callbackHandler, this.sharedState);
        if (!localModule.module.login())
          continue;
        arrayOfInt2[localModule.getFlag()] += 1;
        arrayOfInt1[localModule.getFlag()] += 1;
        if (localModule.getFlag() != 3)
          continue;
      }
      catch (Throwable localThrowable1)
      {
        if (localObject1 == null)
          localObject1 = localThrowable1;
        if (localModule.klass == null)
        {
          arrayOfInt2[1] += 1;
        }
        else
        {
          arrayOfInt2[localModule.getFlag()] += 1;
          if (localModule.getFlag() != 2);
        }
      }
      break;
    }
    int i = 1;
    if ((arrayOfInt1[1] == arrayOfInt2[1]) && (arrayOfInt1[2] == arrayOfInt2[2]))
      if ((arrayOfInt2[1] == 0) && (arrayOfInt2[2] == 0))
      {
        if ((arrayOfInt1[0] != 0) || (arrayOfInt1[3] != 0))
          i = 0;
      }
      else
        i = 0;
    int[] arrayOfInt3 = new int[4];
    int tmp265_264 = (arrayOfInt2[2] = arrayOfInt2[3] = 0);
    arrayOfInt2[1] = tmp265_264;
    arrayOfInt2[0] = tmp265_264;
    Object localObject2;
    if (i == 0)
      for (localObject2 : this.modules)
        if (localObject2.klass != null)
        {
          arrayOfInt2[localObject2.getFlag()] += 1;
          try
          {
            localObject2.module.commit();
            arrayOfInt3[localObject2.getFlag()] += 1;
          }
          catch (Throwable localThrowable2)
          {
            if (localObject1 == null)
              localObject1 = localThrowable2;
          }
        }
    i = 1;
    if ((arrayOfInt3[1] == arrayOfInt2[1]) && (arrayOfInt3[2] == arrayOfInt2[2]))
      if ((arrayOfInt2[1] == 0) && (arrayOfInt2[2] == 0))
      {
        if ((arrayOfInt3[0] != 0) || (arrayOfInt3[3] != 0))
          i = 0;
      }
      else
        i = 0;
    if (i != 0)
    {
      for (localObject2 : this.modules)
        try
        {
          localObject2.module.abort();
        }
        catch (Throwable localThrowable3)
        {
          if (localObject1 == null)
            localObject1 = localThrowable3;
        }
      if (((localObject1 instanceof PrivilegedActionException)) && (((Throwable)localObject1).getCause() != null))
        localObject1 = ((Throwable)localObject1).getCause();
      if ((localObject1 instanceof LoginException))
        throw ((LoginException)localObject1);
      throw ((LoginException)new LoginException("auth.37").initCause((Throwable)localObject1));
    }
    this.loggedIn = true;
  }

  public void logout()
    throws LoginException
  {
    PrivilegedExceptionAction local3 = new PrivilegedExceptionAction()
    {
      public Void run()
        throws LoginException
      {
        LoginContext.this.logoutImpl();
        return null;
      }
    };
    try
    {
      if (this.userProvidedConfig)
        AccessController.doPrivileged(local3, this.userContext);
      else
        AccessController.doPrivileged(local3);
    }
    catch (PrivilegedActionException localPrivilegedActionException)
    {
      throw ((LoginException)localPrivilegedActionException.getException());
    }
  }

  private void logoutImpl()
    throws LoginException
  {
    if (this.subject == null)
      throw new LoginException("auth.38");
    this.loggedIn = false;
    Object localObject = null;
    int i = 0;
    for (Module localModule : this.modules)
      try
      {
        localModule.module.logout();
        i++;
      }
      catch (Throwable localThrowable)
      {
        if (localObject == null)
          localObject = localThrowable;
      }
    if ((localObject != null) || (i == 0))
    {
      if (((localObject instanceof PrivilegedActionException)) && (((Throwable)localObject).getCause() != null))
        localObject = ((Throwable)localObject).getCause();
      if ((localObject instanceof LoginException))
        throw ((LoginException)localObject);
      throw ((LoginException)new LoginException("auth.37").initCause((Throwable)localObject));
    }
  }

  private class ContextedCallbackHandler
    implements CallbackHandler
  {
    private final CallbackHandler hiddenHandlerRef;

    ContextedCallbackHandler(CallbackHandler arg2)
    {
      Object localObject;
      this.hiddenHandlerRef = localObject;
    }

    public void handle(final Callback[] paramArrayOfCallback)
      throws IOException, UnsupportedCallbackException
    {
      try
      {
        AccessController.doPrivileged(new PrivilegedExceptionAction()
        {
          public Void run()
            throws IOException, UnsupportedCallbackException
          {
            LoginContext.ContextedCallbackHandler.this.hiddenHandlerRef.handle(paramArrayOfCallback);
            return null;
          }
        }
        , LoginContext.this.userContext);
      }
      catch (PrivilegedActionException localPrivilegedActionException)
      {
        if ((localPrivilegedActionException.getCause() instanceof UnsupportedCallbackException))
          throw ((UnsupportedCallbackException)localPrivilegedActionException.getCause());
        throw ((IOException)localPrivilegedActionException.getCause());
      }
    }
  }

  private final class Module
  {
    AppConfigurationEntry entry;
    int flag;
    LoginModule module;
    Class<?> klass;

    Module(AppConfigurationEntry arg2)
    {
      Object localObject;
      this.entry = localObject;
      AppConfigurationEntry.LoginModuleControlFlag localLoginModuleControlFlag = localObject.getControlFlag();
      if (localLoginModuleControlFlag == AppConfigurationEntry.LoginModuleControlFlag.OPTIONAL)
        this.flag = 0;
      else if (localLoginModuleControlFlag == AppConfigurationEntry.LoginModuleControlFlag.REQUISITE)
        this.flag = 2;
      else if (localLoginModuleControlFlag == AppConfigurationEntry.LoginModuleControlFlag.SUFFICIENT)
        this.flag = 3;
      else
        this.flag = 1;
    }

    int getFlag()
    {
      return this.flag;
    }

    void create(Subject paramSubject, CallbackHandler paramCallbackHandler, Map<String, ?> paramMap)
      throws LoginException
    {
      String str = this.entry.getLoginModuleName();
      if (this.klass == null)
        try
        {
          this.klass = Class.forName(str, false, LoginContext.this.contextClassLoader);
        }
        catch (ClassNotFoundException localClassNotFoundException)
        {
          throw ((LoginException)new LoginException("auth.39 " + str).initCause(localClassNotFoundException));
        }
      if (this.module == null)
      {
        try
        {
          this.module = ((LoginModule)this.klass.newInstance());
        }
        catch (IllegalAccessException localIllegalAccessException)
        {
          throw ((LoginException)new LoginException("auth.3A " + str).initCause(localIllegalAccessException));
        }
        catch (InstantiationException localInstantiationException)
        {
          throw ((LoginException)new LoginException("auth.3A" + str).initCause(localInstantiationException));
        }
        this.module.initialize(paramSubject, paramCallbackHandler, paramMap, this.entry.getOptions());
      }
    }
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     org.apache.harmony.javax.security.auth.login.LoginContext
 * JD-Core Version:    0.6.1
 */